package cc.autoapi.pucong.auto.flow.core.flow.execNode.utils;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import org.python.core.*;
import org.python.util.PythonInterpreter;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: cong.pu
 * @create: 2024-09-30 09:55
 **/
public class JythonUtils {
    /**
     * 将PyObject对象转成String对象
     *
     * @param pyObject
     * @return
     * @throws Exception
     * @author ZQ
     */
    public static Object toObjectFromPyObject(PyObject pyObject) {
        if (pyObject == null) {
            return null;
        }
        if (pyObject instanceof PyString)
            return (String) pyObject.__tojava__(String.class);
        else if (pyObject instanceof PyList)
            return (List<?>) pyObject.__tojava__(List.class);
        else if (pyObject instanceof PyInteger)
            return (int) pyObject.__tojava__(Integer.class);
        else if (pyObject instanceof PyFloat)
            return (float) pyObject.__tojava__(Float.class);
        else if (pyObject instanceof PyLong)
            return (long) pyObject.__tojava__(Long.class);
        else if (pyObject instanceof PyDictionary)
            return (Map<?, ?>) pyObject.__tojava__(Map.class);
        else if (pyObject instanceof PySet)
            return (Set<?>) pyObject.__tojava__(Set.class);
        else if (pyObject instanceof PyNone)
            return null;
        else
            return null;
    }

    public static PyObject toPyObject(DataTypeEnum dateType, String value) {
        if (dateType == DataTypeEnum.NUMBER) {
            return new PyInteger(Integer.parseInt(value));
        } else if (dateType == DataTypeEnum.STRING) {
            return new PyString(value);
        } else {
            return new PyString(value);
        }
    }

    /**
     * 解决中文乱码问题
     *
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encoding(String str)
            throws UnsupportedEncodingException {
        return new String(str.getBytes("iso8859-1"), "utf-8");
    }

    /**
     * @param filename 文件名
     * @param funName  函数名
     * @param args     可变参数
     * @return
     * @throws Exception
     * @author ZQ
     */
    public static PyObject execPyFunction(String filename, String funName,
                                          Object... args) throws Exception {
        int len = args.length;
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(filename);
        PyFunction function = interpreter.get(funName, PyFunction.class);
        if (len == 0)
            return function.__call__();
        PyObject[] pyObjects = new PyObject[len];
        for (int i = 0; i < pyObjects.length; i++) {
            if (args[i] instanceof Integer) {
                pyObjects[i] = new PyInteger((int) args[i]);
                continue;
            } else if (args[i] instanceof Float) {
                pyObjects[i] = new PyFloat((float) args[i]);
                continue;
            } else if (args[i] instanceof Long) {
                pyObjects[i] = new PyLong((Long) args[i]);
                continue;
            } else if (args[i] instanceof String) {
                pyObjects[i] = new PyString((String) args[i]);
                continue;
            } else if (args[i] instanceof Character) {
                pyObjects[i] = new PyString((char) args[i]);
                continue;
            } else if (args[i] instanceof List<?>) {
                PyList pyList = new PyList(PyType.fromClass(PyInteger.class));
                for (Object o : (List<?>) args[i]) {
                    if (o instanceof Integer) {
                        pyList.append(new PyInteger((int) o));
                        continue;
                    } else if (o instanceof Float) {
                        pyList.append(new PyFloat((Float) o));
                        continue;
                    } else if (o instanceof Long) {
                        pyList.append(new PyLong((Long) o));
                        continue;
                    } else if (o instanceof String) {
                        pyList.append(new PyString((String) o));
                        continue;
                    } else {
                        throw new Exception(
                                "java中List中的子元素只能是int,float,long,String中的!");
                    }
                }
                pyObjects[i] = pyList;
                continue;
            } else {
                throw new Exception("参数类型只能是int,float,long,String,List其中的!");
            }
        }
        return function.__call__(pyObjects);
    }

    /**
     * 执行py文件
     *
     * @param filename
     * @author ZQ
     */
    public static void execPyFile(String filename) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(filename);
    }
}